package com.example.dragandview.recyclerviewplus;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dragandview.R;

import java.util.ArrayList;
import java.util.List;


public class RecyclerPlusActivity extends AppCompatActivity {
    private RecyclerView rv_recyclerplus;
    private List<Integer> mDatas;
     HomeAdapter homeAdapter;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerplus_index);


        initData();
        rv_recyclerplus = findViewById(R.id.rv_recyclerplus);
        homeAdapter = new HomeAdapter(RecyclerPlusActivity.this,mDatas);
        rv_recyclerplus.setAdapter(homeAdapter);
        rv_recyclerplus.addItemDecoration(new ItemDercotion());
        rv_recyclerplus.setLayoutManager(new LinearLayoutManager(RecyclerPlusActivity.this,LinearLayoutManager.VERTICAL,false));



    }
    private class ItemDercotion extends RecyclerView.ItemDecoration{
        Paint mPaint = new Paint();

        public ItemDercotion() {
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.parseColor("#DA034E"));
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            int postion = parent.getChildAdapterPosition(view);
            if (postion != 0){
                outRect.top = 10;;
            }
        }

        @Override
        public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            int pos = parent.getChildCount();
            for (int i = 1 ; i < pos ; i++){
                c.drawRect(0,parent.getChildAt(i-1).getBottom(),parent.getChildAt(i-1).getRight(),parent.getChildAt(i).getTop(),mPaint);
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.recyclerview_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.id_action_gridview:
                rv_recyclerplus.setLayoutManager(new GridLayoutManager(RecyclerPlusActivity.this,2,GridLayoutManager.VERTICAL,false));
                break;
            case R.id.id_action_listview:
//                rv_recyclerplus.setLayoutManager(new LinearLayoutManager(this));
                rv_recyclerplus.setLayoutManager(new LinearLayoutManager(RecyclerPlusActivity.this,LinearLayoutManager.VERTICAL,false));
                break;
        }
        return true;
    }

    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViiewHoler>{

        private List<Integer> mDatas;
        private Context context;

        public HomeAdapter(Context contet, List<Integer> datas) {
            context = contet;
            mDatas = datas;
        }

        @NonNull
        @Override
        public HomeAdapter.MyViiewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView =  LayoutInflater.from(context).inflate(R.layout.recyclerplus_item,parent,false);
//            View itemView = View.inflate(context,R.layout.recyclerplus_item,null);
            return new MyViiewHoler(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull HomeAdapter.MyViiewHoler holder, int position) {
            holder.tv.setText(mDatas.get(position).toString());
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        class MyViiewHoler extends RecyclerView.ViewHolder{
            TextView tv;
            public MyViiewHoler(@NonNull View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv_recyclerplus_item);
            }
        }
    }
    protected void initData() {
        mDatas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mDatas.add(i);
        }
    }
}
